const formatMsgTime = function(dateTimeStamp) {
    var minute =  60;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;

    var now = Date.parse( new Date())/1000;
    var diffValue = now - dateTimeStamp;
    var monthC = diffValue/month;
    var weekC = diffValue/(7*day);
    var dayC = diffValue/day;
    var hourC = diffValue/hour;
    var minC = diffValue/minute;
    let result = null
    if(monthC>=1){
        result=parseInt(monthC) + "个月前";
     }
     else if(weekC>=1){
        result=parseInt(weekC) + "周前";
     }
     else if(dayC>=1){
        result=parseInt(dayC) +"天前";
     }
     else if(hourC>=1){
        result=parseInt(hourC) +"个小时前";
     }
     else if(minC>=1){
        result=parseInt(minC) +"分钟前";
     }else
        result="刚刚更新";
     return result;
}

export default {
    formatMsgTime (timespan){
       return formatMsgTime(timespan)
    },
    formatDateTime(timeStamp,type) {
        var returnType = type || ''
        var date = new Date(timeStamp*1000);
        var y = date.getFullYear();  
        var m = date.getMonth() + 1;  
        m = m < 10 ? ('0' + m) : m;  
        var d = date.getDate();  
        d = d < 10 ? ('0' + d) : d;  
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;  
        second = second < 10 ? ('0' + second) : second;
        if(returnType=='time'){
          return h+':'+minute+':'+second;
        }else if(returnType=='date'){
          return y + '-' + m + '-' + d
        }else{
          return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
        }
       
    },
    /**
   * 判断文件流大小是否超出限制
   * file —— 文件流
   * max —— 文件size可使用范围最大值：如1M传1、2M传2，默认为2M（含2M）
   */
  checkFileSize (file, max) {
    if (!file) {
      return false
    }
    let fileSize = file.size
    // 默认2M
    if (!max) {
      max = 2
    }
    max = max * 1024 * 1024
    if (fileSize <= max) {
      return true
    } else {
      Toast('文件大小超出限制')
      return false
    }
  },
  /**
   * 判断文件流类型是否超出限制
   * file —— 文件流
   * range —— 文件type可使用范围：['image/jpeg', 'image/jpg', 'image/png']，默认为检测后缀名为jpeg、jpg、png的图片，[]表接受全部类型
   */
  checkFileType (file, range) {
    if (!file) {
      return false
    }
    let fileType = file.type
    try {
      if (range.length === 0) {
        return true
      }
    } catch (err) {
      // 默认为检测后缀名为jpeg、jpg、png的图片
      if (!range) {
        range = ['image/jpeg', 'image/jpg', 'image/png']
      }
    }
    for (var i = 0; i < range.length; i++) {
      if (fileType === range[i]) {
        return true
      }
    }
    Toast('文件类型不支持，请重新选择')
    return false
  },
    unique (arr1, arr2,key){ 
      return arr1.filter(function(obj){ 
          let arr = arr2.filter(function(item){
              if(obj[key] == item[key]){
                    return item
              }
          })
          if(arr.length==0){
              return obj
          }
      })   
  },
  compare(prop) {
      return function (obj1, obj2) {
          var val1 = obj1[prop];
          var val2 = obj2[prop];
          if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
              val1 = Number(val1);
              val2 = Number(val2);
          }
          if (val1 > val2) {
              return -1;
          } else if (val1 < val2) {
              return 1;
          } else {
              return 0;
          }            
      } 
  },
  array_diff(a, b) {
    for (var i = 0; i < b.length; i++) {
      for (var j = 0; j < a.length; j++) {
        if (a[j] == b[i]) {
          a.splice(j, 1);
          j = j - 1;
        }
      }
    }
    return a;
  }
  
}